package com.gitee.openviducn.inspector.rest;

import com.gitee.openviducn.inspector.common.BizException;
import com.gitee.openviducn.inspector.common.ResponseResult;
import com.gitee.openviducn.inspector.domain.vo.request.*;
import com.gitee.openviducn.inspector.domain.vo.response.openvidu.RecordingVO;
import com.gitee.openviducn.inspector.manager.RecordManager;
import io.openvidu.java.client.OpenViduHttpException;
import io.openvidu.java.client.OpenViduJavaClientException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: https://gitee.com/wesleyOne
 * @Date: 04.19 2020
 */
@Slf4j
@Api(tags = "录制管理接口")
@RequestMapping("/record/manager")
@RestController
public class RecordController {

    @Resource
    private RecordManager recordManager;

    @ApiOperation("开始录制")
    @PostMapping("/start")
    public ResponseResult<RecordingVO> startRecording(@Validated @RequestBody RecordStartRequest recordStartRequest) throws OpenViduJavaClientException, BizException {
        RecordingVO recordingVO = recordManager.startRecording(recordStartRequest.getHostname(), recordStartRequest.getSessionId());
        return ResponseResult.success(recordingVO);
    }

    @ApiOperation("停止录制")
    @PostMapping("/stop")
    public ResponseResult<RecordingVO> stopRecording(@Validated @RequestBody RecordStopRequest recordStopRequest) throws OpenViduJavaClientException, BizException {
        RecordingVO recordingVO = recordManager.stopRecording(recordStopRequest.getHostname(), recordStopRequest.getRecordingId());
        return ResponseResult.success(recordingVO);
    }

    @ApiOperation("获取录制信息")
    @PostMapping("/get")
    public ResponseResult<RecordingVO> getRecording(@Validated @RequestBody RecordGetRequest recordGetRequest) throws OpenViduJavaClientException, BizException {
        RecordingVO recordingVO = recordManager.getRecording(recordGetRequest.getHostname(), recordGetRequest.getRecordingId());
        return ResponseResult.success(recordingVO);
    }

    @ApiOperation("录制信息列表")
    @PostMapping("/list")
    public ResponseResult<List<RecordingVO>> listRecordings(@Validated @RequestBody RecordListRequest recordListRequest) throws OpenViduJavaClientException, OpenViduHttpException {
        List<RecordingVO> recordingVOList = recordManager.listRecordings(recordListRequest.getHostname());
        return ResponseResult.success(recordingVOList);
    }

    @ApiOperation("删除录制文件")
    @PostMapping("/del")
    public ResponseResult<Void> deleteRecording(@Validated @RequestBody RecordDeleteRequest recordDeleteRequest) throws OpenViduJavaClientException, OpenViduHttpException {
        recordManager.deleteRecording(recordDeleteRequest.getHostname(), recordDeleteRequest.getRecordingId());
        return ResponseResult.success();
    }

}
